package ru.athena.runTool.Model;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.tree.DefaultMutableTreeNode;

import ru.athena.runTool.AppProperties;
import ru.athena.runTool.blockTypes.CodeBlock;
import ru.athena.runTool.blockTypes.Comment;
import ru.athena.runTool.blockTypes.Script;

public class RunToolModel extends Object {
		
	public class FileFilterSqlOrDir implements FileFilter {

		public boolean accept(File f) {
			return f.getName().toLowerCase().endsWith(".sql")
				 	|| f.isDirectory();
			}
	}

	/**
	 * @author corc_usr
	 *
	 */
	private AppProperties prop_;

	/**
	 * @param prop_
	 */
	public RunToolModel(AppProperties prop) {
		this.prop_ = prop;
	}

	/**
	 * @param processParam TODO
	 * @return 
	 * @throws SQLException
	 */
	public void executeCodeTree( CodeTreeProcessParam processParam ) 
	throws Exception {
		
		DefaultMutableTreeNode currNode = (DefaultMutableTreeNode)processParam.startPath.getLastPathComponent();
		Filter 		filter = new FilterFromNode( currNode );
		Operation	execute = new OperationExecute( processParam.conn );

		HierarchicalVisitorFiltered	filteredVisitor = new HierarchicalVisitorFiltered();
		filteredVisitor.add(filter);
		filteredVisitor.add(processParam.showProgress);
		filteredVisitor.add(execute);
		// always accepting root (filtered visitor visit only selected nodes and
		// below)
		((AbstractNode) currNode.getRoot()).accept(filteredVisitor);			

	}

	public void storeCodeTree( CodeTreeProcessParam processParam ) throws Exception {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
		Operation	store = new OperationStore( processParam.conn, dateFormat.format(date) );
		// filtered without filter - process all nodes 
		HierarchicalVisitorFiltered	filteredVisitor = new HierarchicalVisitorFiltered();
		filteredVisitor.add(store);
		filteredVisitor.add(processParam.showProgress);		
		// always accepting root (filtered visitor visit only selected nodes and
		// below)
		((AbstractNode) processParam.startPath.getPathComponent(0)).accept(filteredVisitor);			
	
	}
	
	public DefaultMutableTreeNode buildTreeForFile(String scriptPath) {
		System.out.println( scriptPath );
		CodeBlock rootScript = new Script( new File( scriptPath), scriptPath);
		return rootScript.toTreeNode();
	}

	public DefaultMutableTreeNode buildTreeForDir(String scriptDir) {
		System.out.println( scriptDir );
		File	dir = new File( scriptDir);
		CodeBlock rootComment = new Comment( dir.getName() );
		DefaultMutableTreeNode root = rootComment.toTreeNode(); 
		for ( File f: dir.listFiles( new FileFilterSqlOrDir() ) ) {
			try {
				if ( f.isDirectory() ) {
					root.add( buildTreeForDir( f.getCanonicalPath() ) );
				} else {
					root.add( buildTreeForFile( f.getCanonicalPath()) );					
				}
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
		return root;
	}

}
